/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */
package assignmentbuilder;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Author:  @author keith
 * Date:    May 6, 2009
 */
public class RandomXMLContentFile extends XMLContentFile {

  final static private List<String> progLangs = new ArrayList<String>(new TreeSet<String>(
          Arrays.asList(new String[]{"Java", "C", "C++", "Basic", "Lisp", "Assembler",
            "COBOL", "FORTRAN", "PASCAL", "Modula-2", "SNOBOL", "XSLT", "smalltalk", "Ada",
            "Perl", "Python", "Ruby", "FORTH", "Javascript"})));
  final static private Map<String, String> courses = new HashMap<String, String>();
  final static private List<String> courseList;


  static {
    courses.put("ACS521", "Topics in Computer Graphics");
    courses.put("ACS544", "Performance Modeling and Evaluation of Computer Systems");
    courses.put("ACS560", "Software Engineering");
    courses.put("ACS562", "Systems Analysis and Design");
    courses.put("ACS564", "Human-Computer Interaction");
    courses.put("ACS566", "The Strategic Role of Information Systems");
    courses.put("ACS567", "Software Project Management");
    courses.put("ACS568", "Object-Oriented Systems Development");
    courses.put("ACS573", "Advanced Operating Systems");
    courses.put("ACS574", "Advanced Computer Networks");
    courses.put("ACS575", "Advanced Survey of Database Design");
    courses.put("ACS576", "Distributed Database Systems");
    courses.put("ACS582", "Expert Systems");

    courses.put("CS106", "Introduction to Computers");
    courses.put("CS112", "Survey of Computer Science");
    courses.put("CS114", "Introduction to Visual Basic");
    courses.put("CS155", "COBOL Programming");
    courses.put("CS160", "Introduction to Computer Science I");
    courses.put("CS160H", "Introduction to Computer Science I Honors");
    courses.put("CS161", "Introduction to Computer Science II");
    courses.put("CS170", "C and Data Structures");
    courses.put("CS172", "Introduction to C");
    courses.put("CS203", "Advanced Visual Basic");
    courses.put("CS232", "Introduction to C and Unix");
    courses.put("CS256", "Applications Software Project");
    courses.put("CS260", "Data Structures");
    courses.put("CS270", "Assembly Language");
    courses.put("CS271", "Computer Architecture");
    courses.put("CS274", "Data Communications");
    courses.put("CS280", "Survey of Information Technology");
    courses.put("CS292", "Intermediate Topics in Computer Science");
    courses.put("CS295", "Industrial Practicum");
    courses.put("CS306", "Computers in Society");
    courses.put("CS321", "Introduction to Computer Graphics");
    courses.put("CS331", "Introduction to C++ and Object-Oriented Programming");
    courses.put("CS350", "Programming Language Design");
    courses.put("CS360", "Software Engineering");
    courses.put("CS364", "Introduction to Database Systems");
    courses.put("CS365", "Advanced Database Systems");
    courses.put("CS366", "Structured Analysis Techniques");
    courses.put("CS367", "Structured Design Techniques");
    courses.put("CS368", "Human-Computer Interaction");
    courses.put("CS372", "Web Application Development");
    courses.put("CS374", "Computer Networks");
    courses.put("CS376", "Advanced Computer Architecture");
    courses.put("CS380", "Artificial Intelligence");
    courses.put("CS384", "Numerical Analysis");
    courses.put("CS395", "Industrial Practice I");
    courses.put("CS421", "Advanced Computer Graphics");
    courses.put("CS445", "Computer Security");
    courses.put("CS460", "Capstone Design and Professional Practice");
    courses.put("CS464", "Computer Systems Planning");
    courses.put("CS466", "Strategic Issues for Information Systems");
    courses.put("CS467", "Project Management");
    courses.put("CS472", "Operating Systems Design");
    courses.put("CS474", "Compiler Construction");
    courses.put("CS486", "Analysis of Algorithms");
    courses.put("CS488", "Theory of Computation");
    courses.put("CS492", "Topics in Computer Science");
    courses.put("CS494", "Directed Study");
    courses.put("CS495", "Cooperative Experience");
    courses.put("CS514", "Numerical Analysis");
    courses.put("CS520", "Computational Methods in Analysis");
    courses.put("CS543", "Introduction to Simulation and Modeling of Computer Systems");
    courses.put("CS547",
            "Information Storage and Retrieval and Natural Language Processing");
    courses.put("CS572", "Heuristic Problem Solving");
    courses.put("CS580", "Algorithm Design Analysis and Implementation");
    courses.put("CS590", "Topics in Computer Science");

    courseList = new ArrayList<String>(courses.keySet());
  }
  final static private List<String> topics = new ArrayList<String>(new TreeSet<String>(
          Arrays.asList(new String[]{"loops", "accumulation", "average", "mean",
            "random numbers", "linked list", "database", "uml", "user interface", "ui",
            "AI", "artificial intelligence", "eclipse", "HTML", "web", "SOA",
            "service oriented architecure", "OO", "OOD", "OOA", "OOAD", "Object oriented",
            "object oriented programming", "Object orient design",
            "Object orient analysis", "Object orient analysis design",
            "polymophism", "cohension", "coupling", "network", "game",
            "heuristic", "algorithm", "parsing"})));
  final static private List<String> prereqs = new ArrayList<String>(new TreeSet<String>(
          Arrays.asList(new String[]{"main programs", "variables",
            "assignment statements",
            "files", "gui", "data structures", "abstract data types", "copy constructor",
            "numerlogy",
            "astrophysics", "horoscopes", "prognostication", "XML schema", "dtd"})));
  final static private List<String> starfleetNames = new ArrayList<String>();
  final static private List<String> suffix = new ArrayList<String>(new TreeSet<String>(
          Arrays.asList(new String[]{"Exercise", "Experiment", "Assignment", "Project", "Lab",
            "Problem", "Report", "Simulation"})));


  static {
    LineNumberReader rdr = null;
    try {
      final File f = new File("starfleet.txt");
      rdr = new LineNumberReader(new FileReader(f));

      String s = null;
      final Set<String> t = new TreeSet<String>();

      while (null != (s = rdr.readLine())) {
        t.add(s.trim());
      }

      starfleetNames.addAll(t);
    } catch (IOException ex) {
      Logger.getLogger(RandomXMLContentFile.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        if (rdr != null) {
          rdr.close();
        }
      } catch (IOException ex) {
        Logger.getLogger(RandomXMLContentFile.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
  final static private List<Author> authors = new ArrayList<Author>();


  static {
    authors.add(defaultAuthor);

    for (char k = 'a'; k <= 'z'; k++) {
      authors.add(new Author(String.valueOf(k), String.valueOf(k),
              String.valueOf(k) + "@acm.org", "1 234 567-8901",
              "IPFW", "http://www.ipfw.edu/~" + String.valueOf(k)));
    }
  }

  public RandomXMLContentFile(String name) {
    super(name);
  }

  private Set<String> choose(List<String> population, double factor) {
    final int count = (int) (Math.random() * factor) + 1;
    final Set<String> t = new HashSet<String>();

    for (int x = 0; x < count; x++) {
      final int pick = (int) (Math.random() * population.size());
      t.add(population.get(pick));
    }

    return t;
  }

  @Override
  protected String getCourseList() {
    final StringBuffer sb = new StringBuffer();
    for (String key : choose(courseList, 3)) {
      if (sb.length() > 0) {
        sb.append(", ");
      }
      sb.append(key).append(": ").append(courses.get(key));
    }

    return sb.toString();
  }

  @Override
  protected String getPrerequisites() {
    final StringBuffer sb = new StringBuffer();
    for (String key : choose(prereqs, 3 * prereqs.size() / 8)) {
      if (sb.length() > 0) {
        sb.append(", ");
      }
      sb.append(key);
    }

    return sb.toString();
  }

  @Override
  protected String getTopics() {
    final StringBuffer sb = new StringBuffer();
    for (String key : choose(topics, 3 * topics.size() / 4)) {
      if (sb.length() > 0) {
        sb.append(", ");
      }
      sb.append(key);
    }

    return sb.toString();
  }

  @Override
  protected String getProgrammingLanguage() {
    final int pick = (int) (Math.random() * progLangs.size());

    return progLangs.get(pick);
  }

  @Override
  protected String getTitle() {
    final int pick1 = (int) (Math.random() * starfleetNames.size());
    final int pick2 = (int) (Math.random() * suffix.size());

    return starfleetNames.get(pick1) + " " + suffix.get(pick2);
  }

  @Override
  protected List<Author> getAuthors() {
    final int count = (int) (Math.random() * 3) + 1;
    final Set<Author> t = new HashSet<Author>();

    for (int x = 0; x < count; x++) {
      final int pick = (int) (Math.random() * authors.size());
      t.add(authors.get(pick));
    }


    return new ArrayList<Author>(t);
  }
}
